package ev.operator;//need to add this line

import ev.data.*;//need to add this line
import java.util.Random;

/*
 * Pick two alleles at random and swap their positions
 * Preserves most of adjacency information (4 links broken), disrupts
 * order more
 */
public class Swap implements Mutation{

	public Individual mutate(Individual old) {
		int[] list=old.getList();
		int len=list.length;

		//generate two distinct positions randomly
		Random r=new Random();
		int start=r.nextInt(len);
		int end=r.nextInt(len);

		//make sure they are distinct
		while(start==end){
			end=r.nextInt(len);
		}

		//swap them
		int temp=list[start];
		list[start]=list[end];
		list[end]=temp;

		//set list and return
		old.setList(list);
		return old;
	}
}
